package com.TanNgee;

import com.TanNgee.util.JedisUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisDataException;

/**
 * @author TanNgee
 * @create 2021/11/29 18:46
 */
public class Service {
    private String id;
    private int num;

    public Service(String id, int num) {
        this.num = num;
        this.id = id;
    }

    // 控制单元
    public void service() {
        Jedis jedis = JedisUtils.getJedis();

//        Jedis jedis = new Jedis("127.0.0.1", 6379);
        String value = jedis.get("compid:" + id);
        // 判断该值是否存在
        try {
            if (value == null) {
                // 不存在，创建该值
                jedis.setex("compid:" + id, 5, Long.MAX_VALUE - num + "");
            } else {
//            存在,自增，调用业务

                Long val = jedis.incr("compid:" + id);
                business(id, num - (Long.MAX_VALUE - val));
            }
        } catch (JedisDataException e) {
//                e.printStackTrace();
            System.out.println("使用以及到达次数上线，请充钱变强");
            return;
        } finally {
            jedis.close();

        }


    }


    // 业务操作
    public void business(String id, Long val) {
        System.out.println("用户：" + id + "业务操作执行次数：" + val);
    }

}

class MyThread extends Thread {
    Service sc;

    public MyThread(String id, int num) {
        sc = new Service(id, num);
    }

    @Override
    public void run() {

        while (true) {
            sc.service();
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Main {
    public static void main(String[] args) {
        MyThread mt1 = new MyThread("初级用户", 10);
        MyThread mt2 = new MyThread("高级用户", 30);
        mt1.start();
        mt2.start();
    }
}
